<?php

namespace my;

class JRedis
{
    protected static $handler = null;
    //创建静态私有的变量保存该类对象
    static private $instance;

    protected $options = [
        'host' => '127.0.0.1',
        'port' => 6379,
        'password' => 'Go@2022',
        'select' => 0,
        'timeout' => 0,
        'expire' => 0,
        'persistent' => false,
        'prefix' => '',
    ];

    private function __construct($options = [])
    {
        if (!extension_loaded('redis')) {   //判断是否有扩展(如果你的apache没reids扩展就会抛出这个异常)
            throw new \BadFunctionCallException('not support: redis');
        }
        if (!empty($options)) {
            $this->options = array_merge($this->options, $options);
        }
        $func = $this->options['persistent'] ? 'pconnect' : 'connect';     //判断是否长连接
        self::$handler = new \Redis;
        self::$handler->$func($this->options['host'], $this->options['port'], $this->options['timeout']);
        if ('' != $this->options['password']) {
            self::$handler->auth($this->options['password']);
        }
        if (0 != $this->options['select']) {
            self::$handler->select($this->options['select']);
        }
    }

    static public function client($config = [])
    {
        //判断$instance是否是唯一的对象
        //没有则创建
        if (!self::$instance instanceof self) {
            self::$instance = new self($config);
        }
        return self::$handler;
    }

    static public function set($key, $value, $expire = 0)
    {
        if (is_object($value) || is_array($value)) {
            $value = json_encode($value);
        }
        if ($expire == 0) {
            $set = self::client()->set($key, $value);
        } else {
            $set = self::client()->setex($key, $expire, $value);
        }
        return $set;
    }

    public static function setNx($key, $value, $expire = 0)
    {
        if (is_object($value) || is_array($value)) {
            $value = json_encode($value);
        }
        $set = self::client()->setNx($key, $value);
        if ($expire != 0 && $set) {
            self::client()->expire($key, $expire);
        }
        return $set;
    }

    public static function expire($key, $expire = 0)
    {
        self::client()->expire($key, $expire);
    }

    public static function get($key)
    {
        $fun = is_array($key) ? 'Mget' : 'get';
        $value = self::client()->{$fun}($key);
        $value_serl = @json_decode($value, true);
        if (is_object($value_serl) || is_array($value_serl)) {
            return $value_serl;
        }
        return $value;
    }

    public static function delete($key)
    {
        return self::client()->del($key);
    }

    public static function lLen($key)
    {
        return self::client()->lLen($key);
    }

    public static function lPush($key, $value)
    {
        if (is_object($value) || is_array($value)) {
            $value = json_encode($value);
        }
        return self::client()->lPush($key, $value);
    }

    public static function lPop($key)
    {
        $value = self::client()->lPop($key);
        $value_serl = @json_decode($value, true);
        if (is_object($value_serl) || is_array($value_serl)) {
            return $value_serl;
        }
        return $value;
    }

    public static function rPop($key)
    {
        $value = self::client()->rPop($key);
        $value_serl = @json_decode($value, true);
        if (is_object($value_serl) || is_array($value_serl)) {
            return $value_serl;
        }
        return $value;
    }

    public static function lRange($key, $start, $end)
    {
        return self::client()->lRange($key, $start, $end);
    }





}
